package nii.game.mobileserio.action_game;

import nii.game.mobileserio.dialog.DialogExit;
import nii.game.mobileserio.dialog.DialogNhom;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;

public class MenuActivity extends Activity {
	
	// Hiệu ứng xoay các biểu tượng PLAY, OPTION, INFO, EXIT
	Animation animation_xoay;
	Animation animation_alpha;
	int index = 0; // Biến trạng thái để điều chỉnh xoay
	
	//===========================|| onCreate Function ||===============================//
	
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(R.layout.activity_main_game);
		
		animation_xoay = AnimationUtils.loadAnimation(this, R.anim.anim_xoay);
		
		// Hình ảnh cho button PLAY
		final ImageButton ImageButton_play = (ImageButton) findViewById(R.id.play);
		ImageButton_play.startAnimation(animation_xoay);
		
		//=======================|| touch PLAY button event||==================================//
		
		ImageButton_play.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View arg0) {
				Intent i = new Intent(MenuActivity.this, MainGameActivity.class);
				MenuActivity.this.startActivity(i);
				MenuActivity.this.finish();

			}
		});
		
		//=======================|| touch INFO button event||==================================//
		
		final ImageButton ImageButton_infor = (ImageButton) findViewById(R.id.info);
		ImageButton_infor.startAnimation(animation_xoay);
		ImageButton_infor.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				DialogNhom dia = new DialogNhom(MenuActivity.this);
				dia.show();
			}
		});

		//=======================|| touch EXIT button event||==================================//
		
		final ImageButton ImageButton_exit = (ImageButton) findViewById(R.id.exit);
		ImageButton_exit.startAnimation(animation_xoay);
		ImageButton_exit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				
				DialogExit dia = new DialogExit(MenuActivity.this, MenuActivity.this);
				dia.show();
			}
		});
		
		//=======================|| touch OPTION button event||================================//		

		final ImageButton ImageButton_option = (ImageButton)findViewById(R.id.option);
		ImageButton_option.startAnimation(animation_xoay);
		ImageButton_option.setOnClickListener(new View.OnClickListener() 
		{
			public void onClick(View v) {
				
				Intent i = new Intent(MenuActivity.this, Option.class);
				MenuActivity.this.startActivity(i);
				MenuActivity.this.finish();
			}
		});
		
		//==========================|| XỬ LÍ HIỆU ỨNG XOAY CÁC BIỂU TƯỢNG ||==============================//
		
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) 
			{
				switch (index) 
				{
					case 0:
						ImageButton_exit.clearAnimation();
						ImageButton_infor.clearAnimation();
						ImageButton_option.clearAnimation();
						ImageButton_play.startAnimation(animation_xoay);
						break;
					case 1:
						ImageButton_play.clearAnimation();
						ImageButton_infor.clearAnimation();
						ImageButton_exit.clearAnimation();
						ImageButton_option.startAnimation(animation_xoay);
						break;
					case 2:
						ImageButton_exit.clearAnimation();
						ImageButton_play.clearAnimation();
						ImageButton_option.clearAnimation();
						ImageButton_infor.startAnimation(animation_xoay);
						break;
					case 3:
						ImageButton_option.clearAnimation();
						ImageButton_infor.clearAnimation();
						ImageButton_play.clearAnimation();
						ImageButton_exit.startAnimation(animation_xoay);
						break;
					default: break;
				}
				super.handleMessage(msg);
			}
		};

		Thread th = new Thread(new Runnable() 
		{
			
			public void run() 
			{
				while (true) 
				{
					try {
						Thread.sleep(4000);
					} 
					catch (InterruptedException e) {
						e.printStackTrace();
					}
					// Xoay luân phiên theo vòng
					index++;
					if (index > 3)
						index = 0;
					Message msg = handler.obtainMessage();
					handler.sendMessage(msg);
				}
			}
		});
		th.start();
	}


}
